package com.cuitrip.business.home.favorite.ui;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.FrameLayout;

import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.utils.DisplayUtil;


/**
 * 收藏动画
 * Created by ak
 */
public class FavAnimation implements Animator.AnimatorListener {

    private Window mWindow;
    private Context mContext;
    private AnimatorSet mAnimatorSet;
    private IconTextView mImage;

    public FavAnimation(Window window) {
        mWindow = window;
        mContext = window.getContext();

        mImage = new IconTextView(mWindow.getContext());
        mImage.setTextSize(21.f);
        mImage.setTextColor(ResourceUtil.getColor(R.color.orange_ff6855));
        mImage.setText(getTextResource());
        mImage.setVisibility(View.GONE);

        int translationY1 = 0;
        int translationY2 = -(int) DisplayUtil.dip2px(40);
        int translationY3 = -(int) DisplayUtil.dip2px(60);

        mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(
                //隐藏歌曲信息
                ObjectAnimator.ofFloat(mImage, "alpha", 0, 1f, 0),
                ObjectAnimator.ofFloat(mImage, "translationY", translationY1, translationY2, translationY3),
                ObjectAnimator.ofFloat(mImage, "scaleX", 0.8f, 2.5f, 1.5f),
                ObjectAnimator.ofFloat(mImage, "scaleY", 0.8f, 2.5f, 1.5f)
        );
        mAnimatorSet.setDuration(800);
        mAnimatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
    }

    public int getTextResource(){
        return R.string.fav_full_icon;
    }

    public void attach() {
        ViewGroup viewGroup = (ViewGroup) mWindow.getDecorView();
        viewGroup.addView(mImage);
    }

    public void detach() {
        mAnimatorSet.cancel();
        ViewGroup viewGroup = (ViewGroup) mWindow.getDecorView();
        viewGroup.removeView(mImage);
    }

    public void show(View locationView) {
        int[] location = new int[2];
        locationView.getLocationOnScreen(location);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(locationView.getWidth(), locationView.getHeight());
        params.leftMargin = location[0];
        params.topMargin = location[1];
        mImage.setLayoutParams(params);
        mAnimatorSet.cancel();
        mImage.clearAnimation();
        mAnimatorSet.start();
        mImage.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        mImage.setVisibility(View.GONE);
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        mImage.setVisibility(View.GONE);
    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }
}
